package com.lc.hongtai.fragment;


import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.holder.Holder;
import com.facebook.drawee.view.SimpleDraweeView;
import com.flyco.tablayout.SlidingTabLayout;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.lc.hongtai.MyApplication;
import com.lc.hongtai.R;
import com.lc.hongtai.activity.AdvertisingActivity;
import com.lc.hongtai.adapter.FragmentAdapter2;
import com.lc.hongtai.conn.JsonAccessToken;
import com.lc.hongtai.conn.JsonEconomics;
import com.lc.hongtai.toast.LovelyToast;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.bound.BoundViewHelper;
import com.zcx.helper.http.AsyCallBack;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;

/**
 * 发现
 *
 * @author Ansen
 * @create time 2015-09-08
 */
public class DynamicFragment extends BaseFragment implements View.OnClickListener, OnTabSelectListener {
    private ViewPager vp_FindFragment_pager;
    private FragmentPagerAdapter fAdapter;
    private List<Fragment> list_fragment;
    private List<String> list_title;
    private Fragment1 fragment1;
    private Fragment2 fragment2;
    SlidingTabLayout tl_2;

    @BoundView(R.id.convenientBanner)
    ConvenientBanner convenientBanner;

    List<String> banner = new ArrayList<>();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = BoundViewHelper.boundView(this, MyApplication.scaleScreenHelper.loadView((ViewGroup) inflater.inflate(R.layout.fragment_second, null)));
        ButterKnife.bind(this, view);

        vp_FindFragment_pager = (ViewPager) view.findViewById(R.id.vp);
        tl_2 = (SlidingTabLayout) view.findViewById(R.id.tl_2);

        initControls();
        getAccessToken();



        return view;
    }


    public void getFindData(){

    }

    private void getAccessToken() {
        new JsonAccessToken("index/find", new AsyCallBack<JsonAccessToken.Info>() {
            @Override
            public void onSuccess(String toast, int type, JsonAccessToken.Info info) throws Exception {
                if (info != null) {
                    if (!TextUtils.isEmpty(info.access_token)) {
                        getData(info.access_token);
                    } else {
                        // UtilToast.show(getActivity(), "安全验证失败！");
                        LovelyToast.makeText(getActivity(), "安全验证失败！", 0, LovelyToast
                                .SUCCESS, LovelyToast.SCALE);
                    }
                }
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                //  UtilToast.show(getActivity(), "安全验证失败！");
                LovelyToast.makeText(getActivity(), "安全验证失败！", 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);
            }
        }).execute(getActivity(), false);
    }

    private void initControls() {

        fragment1 = new Fragment1();
        fragment2 = new Fragment2();


        list_fragment = new ArrayList<>();

        list_fragment.add(fragment1);
        list_fragment.add(fragment2);


        list_title = new ArrayList<>();
        list_title.add("经济");
        list_title.add("生活");


        fAdapter = new FragmentAdapter2(getActivity().getSupportFragmentManager(), list_fragment, list_title);

        vp_FindFragment_pager.setAdapter(fAdapter);

        tl_2.setViewPager(vp_FindFragment_pager);
        tl_2.setOnTabSelectListener(this);

    }


    @Override
    public void onTabSelect(int position) {
        Toast.makeText(getActivity(), "onTabSelect&position--->" + position, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onTabReselect(int position) {
        Toast.makeText(getActivity(), "onTabReselect&position--->" + position, Toast.LENGTH_SHORT).show();
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @Override
    public void onClick(View v) {

    }


    @Override
    public void onResume() {
        super.onResume();
        convenientBanner.startTurning(3000);
    }

    // 停止自动翻页
    @Override
    public void onPause() {
        super.onPause();
        //停止翻页
        convenientBanner.stopTurning();
    }

    public class LocalImageHolderViewMiddle implements Holder<JsonEconomics.Info.Banner> {
        private SimpleDraweeView imageView;

        @Override
        public View createView(Context context) {
            imageView = new SimpleDraweeView(context);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            return imageView;
        }

        @Override
        public void UpdateUI(Context context, final int position, final JsonEconomics.Info.Banner listContent) {
            imageView.setImageURI(Uri.parse(listContent.img));
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent();
                    intent.putExtra("title", listContent.title);
                    intent.putExtra("url", listContent.url);

                    Log.d(listContent.url, "88888888888888888888888888");
                    intent.setClass(getActivity(), AdvertisingActivity.class);
                    getActivity().startActivity(intent);
                }
            });


        }
    }



    public void getData(String access_token) {
        new JsonEconomics(access_token, 2, new AsyCallBack<JsonEconomics.Info>() {

            @Override
            public void onStart(int type) throws Exception {
                super.onStart(type);
                showLoading();
            }

            @Override
            public void onSuccess(String toast, int type, JsonEconomics.Info info) throws Exception {
                super.onSuccess(toast, type, info);
                convenientBanner.setPages(
                        new CBViewHolderCreator<DynamicFragment.LocalImageHolderViewMiddle>() {
                            @Override
                            public DynamicFragment.LocalImageHolderViewMiddle createHolder() {
                                return new DynamicFragment.LocalImageHolderViewMiddle();
                            }
                        }, info.banner).setPageIndicator(new int[]{R.mipmap.ic_dots_uncheck, R.mipmap.ic_dots_check})
                        //设置指示器的方向
                        .setPageIndicatorAlign(ConvenientBanner.PageIndicatorAlign.CENTER_HORIZONTAL).setCanLoop(true);
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                dismissLoading();
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
                dismissLoading();
            }
        }


        ).execute(getActivity(), false);


    }



}
